<template>
  <div class="">
    <a-modal
      v-model:visible="visible"
      :title="dialogTitle"
      :modal-style="modalStyle"
      :body-style="bodyStyle"
      :mask-closable="false"
      @cancel="dialogCancel"
      @before-ok="dialogBeforeOk"
    >
      <a-form ref="formRef" :model="formModel">
        <a-form-item field="username" label="账号">
         <a-input v-model="formModel.username" disabled style="width: 80%"/>
        </a-form-item>
        <a-form-item field="nickname" label="昵称">
          <a-input v-model="formModel.nickname" disabled style="width: 80%"/>
        </a-form-item>
        <a-form-item
          field="password"
          label="密码"
          required
          :validate-trigger="['change','input']"
          :rules="[
            {
              required: true,
              message: '密码不能为空',
            },
          ]"
        >
          <a-input-password v-model="formModel.password" placeholder="请输入新密码" autocomplete="off" allow-clear style="width: 80%"/>
        </a-form-item>
        <a-form-item>
          <PasswordStrength v-model="formModel.password"/>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script setup>
  import { ref } from 'vue';

  import useVisible from '@/hooks/visible';
  import { apiSetPwd } from '@/api/admin';
  import PasswordStrength from '@/components/password-strength/index.vue';

  const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '',
    },
    id: {
      type: [Number, String],
      default: '',
    },
  });
  const emit = defineEmits(['saveCallback']);

  const baseFormModel = () => {
    return {
      id: '',
      username: '',
      nickname: '',
      password: '',
      status: 1,
    };
  };
  const formRef = ref();
  const formModel = ref(baseFormModel());
  const modalStyle = {
    width: '450px',
    height: '370px',
  };
  const bodyStyle = {
    height: 'calc(100% - 108px)',
    overflow: 'auto',
  };

  const { visible, setVisible } = useVisible(false);
  const dialogTitle = ref();

  // 表单重置
  const reset = async () => {
    formModel.value = baseFormModel();
    await formRef.value?.resetFields();
  };
  const fetchData = async (row) => {
    setVisible(true);
    dialogTitle.value = '设置密码';

    await reset();
    if (row) {
      formModel.value = row;
      formModel.value.password = '';
    }
  };
  // 提交
  const submit = async (done) => {
    try {
      const ruleRes = await formRef.value?.validate();
      if (!ruleRes) {
        await apiSetPwd(formModel.value);
        done();
        emit('saveCallback');
      } else {
        done(false);
      }
    } catch (err) {
      done(false);
    } finally {
      done(false);
    }
  };

  const dialogCancel = () => {
    setVisible(false);
  };
  const dialogBeforeOk = (done) => {
    submit(done);
  };

  // 暴露方法【给父级调用】
  defineExpose({
    fetchData,
  });
</script>

<style scoped lang="less">
  .arco-col .arco-input-wrapper {
    width: 100% !important;
  }
</style>
